//
//  ContentTableViewCell.m
//  Cunli
//
//  Created by 湛亚 on 15/9/28.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ContentTableViewCell.h"
#import "UIImageView+EMWebCache.h"
#import "PointsManager.h"
#import "ConvertToCommonEmoticonsHelper.h"
#import "LinLi.h"
#import "TTTAttributedLabel.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@implementation ContentTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier andLinLi:(LinLi *)linLi{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _headImg=[[UIImageView alloc]init];
        _headImg.frame=CGRectMake(10, 10, 40, 40);
        _headImg.layer.cornerRadius = 20;
        _headImg.clipsToBounds = YES;
        _headImg.userInteractionEnabled=YES;
        if ([PointsManager isDefaultTouXiangWithUrlstring:linLi.txpic]) {
            _headImg.image=[UIImage imageNamed:@"default_touxiang.png"];
            
        }else{
            [_headImg sd_setImageWithURL:[NSURL URLWithString:linLi.txpic] placeholderImage:[UIImage imageNamed:@"default_touxiang.png"]];
        }
        [self.contentView addSubview:_headImg];
        
        _namelabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 15, 22*(linLi.truename.length), 30)];
        _namelabel.text=linLi.truename;
        _namelabel.userInteractionEnabled=YES;
        _namelabel.textColor=[UIColor colorWithRed:18/255.0 green:133/255.0 blue:210/255.0 alpha:1];
        [self.contentView addSubview:_namelabel];
        
        NSString *contentText = [ConvertToCommonEmoticonsHelper
                                 convertToSystemEmoticons:linLi.introduce];
        float height=[self heightWithString:contentText];
        _contentLabel=[PointsManager createWithframe:CGRectMake(60, 47, SCREEN_WIDTH-70, height) andString:contentText];
        [self.contentView addSubview:_contentLabel];
        
        height=height+50;
        float imgWidth=(SCREEN_WIDTH-90)/3.0;
        height=height+(linLi.pics.count+2)/3*(imgWidth-5);
        
        UILabel *timeLab=[[UILabel alloc]initWithFrame:CGRectMake(60, height+1, 120, 13)];
        timeLab.textColor=[UIColor grayColor];
        timeLab.text=linLi.addtime;
        timeLab.font=[UIFont systemFontOfSize:14];
        [self.contentView addSubview:timeLab];
        
        _commentImg=[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-120, height+2, 43, 14)];
        //helpImgPic.image=[UIImage imageNamed:@"帮忙.png"];
        [self.contentView addSubview:_commentImg];
        
        _zanImg=[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-48, height+1, 33, 14)];
        _zanImg.image=[UIImage imageNamed:@"赞.png"];
        _zanImg.userInteractionEnabled=YES;
        [self.contentView addSubview:_zanImg];
    }
    return self;
}

-(float)heightWithString:(NSString *)introduce{
    
    CGRect rect = [introduce boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-70, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]} context:nil];
    CGFloat height = ceilf(rect.size.height);
    return height + 10;
}

@end
